﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Tb.App.API.Applications.Command.UploadImage;
using Tb.App.API.Applications.DTOs;
using Tb.App.Domain;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;

namespace Tb.App.API.Applications.CommandHandler.UploadImage
{
    /// <summary>
    /// 查询图片列表查询命令
    /// </summary>
    public class GetBatchImagesQueryCommandHandler : IRequestHandler<GetBatchImagesQueryCommand, ApiResult<List<BatchImageDto>>>
    {
        private readonly IBaseRepository<Images> imagesRep;

        public GetBatchImagesQueryCommandHandler(IBaseRepository<Images> imagesRep)
        {
            this.imagesRep = imagesRep;
        }
        public async Task<ApiResult<List<BatchImageDto>>> Handle(GetBatchImagesQueryCommand request, CancellationToken cancellationToken)
        {
            var images = await imagesRep.GetAll()
               .Where(x => x.BatchNo == request.BatchNo && x.ImageType == request.ImageType&&!x.IsDel)
               .Select(x => new BatchImageDto
               {
                   Id = x.Id,
                   ImagePath = x.ImagePath
               })
               .ToListAsync(cancellationToken);
            return new ApiResult<List<BatchImageDto>>
            {
                Code = ApiEnum.查询成功,
                Message = "查询成功",
                Data = images
            };
        }
    }
}
